<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>
        blogs
    </title>
    
<link rel="stylesheet" href="/libs/highlight/styles/monokai-sublime.css">

    
<link rel="stylesheet" href="/libs/font-awesome/css/font-awesome.min.css">

    
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 4.2.0"></head>

<body id="bodyx">
    <div class="hd posts">
    <a href="/index.html"><i class="fa fa-reply replay-btn" aria-hidden="true"></i></a>
    <div class="post-title">
        <p>
            学习ES6语法(2)
        </p>
        <hr>
    </div>
    <div class="post-content">
        <h1 id="二、新的声明方式"><a href="#二、新的声明方式" class="headerlink" title="二、新的声明方式"></a>二、新的声明方式</h1><p>说明：在未学习ES6语法前，我们常用的声明方式只有一种<strong>var</strong>，使用var最大不好之处就是那是全局声明，不存在作用域的，除非闭包</p>
<ul>
<li><p>字面理解ES6的三种声明方式：</p>
<ul>
<li><code>var</code>：它是variable的简写，可以理解成变量的意思。</li>
<li><code>let</code>：它在英文中是“让”的意思，也可以理解为一种声明的意思</li>
<li><code>const</code>：它在英文中也是常量的意思，在ES6也是用来声明常量的，常量你可以简单理解为不变的量</li>
</ul>
</li>
<li><p><strong>var声明：</strong></p>
<ul>
<li>var在ES6里是用来升级全局变量的，我们可以先作一个最简单的实例</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">我们使用<span class="keyword">var</span>来做一个声明</span><br><span class="line"><span class="keyword">var</span> a = <span class="string">"yuynag"</span></span><br><span class="line"><span class="built_in">console</span>.log(a);</span><br><span class="line"></span><br><span class="line">浏览器控制台打印了 yuyang</span><br></pre></td></tr></table></figure>

<ul>
<li>如何理解它的作用是声明全局变量那？</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">我们使用一个匿名函数进行包裹，在匿名函数中看这个变量是否会被打印</span><br><span class="line"><span class="keyword">var</span> a = <span class="number">45</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">window</span>.onload = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(a)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">结果是浏览器输出了定义的这个变量参数<span class="number">45</span>，还不足以说明他是作用全局的</span><br><span class="line"></span><br><span class="line">我们定义一个块级参数，和一个非块级参数，看输出的结果是？</span><br><span class="line"><span class="keyword">var</span> a = <span class="number">10</span></span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">var</span> a = <span class="number">15</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(a)</span><br><span class="line"></span><br><span class="line">最总打印的结果值为 <span class="number">15</span>，由此说明它是一个全局</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>let局部</strong></p>
<ul>
<li>我们对var的全局声明有了一定了解。那跟var向对应的是let，它是局部变量声明（还是以上面的案例为主）</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> a = <span class="number">10</span>;</span><br><span class="line"> &#123;</span><br><span class="line">     <span class="keyword">let</span> a = <span class="number">15</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> </span><br><span class="line"> <span class="built_in">console</span>.log(a)</span><br><span class="line"> </span><br><span class="line"> 使用<span class="keyword">let</span>定义参数变量之后他就存在一定的作用域了，然而此时输出的肯定是<span class="number">10</span>了，块级中定义的参数是不能被调用的</span><br><span class="line"> </span><br><span class="line">如果只在块级中声明，不再外声明，那也是调用不上的，此时浏览器就会报错，说我们没有定义就调用</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">let</span> a = <span class="number">10</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="number">10</span>)</span><br></pre></td></tr></table></figure>

<ul>
<li>综上两个案例进行对比，let是具有作用域的，局部变量声明，let旨在作用块起作用，外部不能被调用</li>
<li>有些刚接触JavaScript的小伙伴会疑惑了，我感觉let还没有var好用，其实let是防止你的数据污染的，在大型项目中是非常有用处的。现在看一个循环的例子，我们来看一下let的好处</li>
<li><strong>var循环和let循环对比</strong></li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">for(var i&#x3D;0;i&lt;10;i++)&#123;</span><br><span class="line">    console.log(&quot;第&quot;+i+&quot;个&quot;);</span><br><span class="line">&#125;</span><br><span class="line">console.log(&quot;第var&quot;+i+&quot;个。。。。&quot;);</span><br><span class="line">在循环体外打印的这个i 会被执行，因为我们使用的var来进行一个声明定义，var声明回去污染全局空间</span><br><span class="line"></span><br><span class="line">for(let i&#x3D;0;i&lt;10;i++)&#123;</span><br><span class="line">    console.log(&quot;第&quot;+i+&quot;个&quot;);</span><br><span class="line">&#125;</span><br><span class="line">console.log(&quot;第var&quot;+i+&quot;个。。。。&quot;);</span><br><span class="line"></span><br><span class="line">使用let方式声明的话，最后一个打印语句中的i会出现报错，i is not defined  i没有被定义，也就是上面说的未被定义就被调用</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>注意点</strong>：通过两种声明的比较，可以明白let在防止程序数据污染上还是很有用处的。我们要努力去习惯用let声明，减少var声明去污染全局空间，在vue的使用中也要注意这点</li>
</ul>
</li>
</ul>
<ul>
<li><p><strong>const声明常量</strong></p>
<ul>
<li>在程序开发中，有的参数希望被定义成一个常量，不可被更改的参数，那就需要const来定义</li>
</ul>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> a = <span class="number">15</span></span><br><span class="line">    a = <span class="number">20</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(a);</span><br><span class="line"></span><br><span class="line">这段代码是一种错误的方式，我们不能去改变a所定义的参数，这样在编译器中是不会报语法错误，但是在转换语法打包的时候就会出现错误 </span><br><span class="line">const  # 很好理解，如果有过Java基础的同学就更能很好理解这个常量  他就和final一样的意思</span><br></pre></td></tr></table></figure></li>
</ul>

    </div>
    
        <hr class="fhr">
        <div id="vcomments"></div>
    
</div>
    <div class="footer" id="footer">
    <p>Copyright © 2020 <a class="flink" href="https://hexo.io" target="_blank" rel="noopener">Hexo</a>-<a class="flink" href="https://github.com/sanjinhub/hexo-theme-geek" target="_blank" rel="noopener">Geek</a>.
        <label class="el-switch el-switch-green el-switch-sm" style="vertical-align: sub;">
            <input type="checkbox" name="switch" id="update_style">
            <span class="el-switch-style"></span>
        </label>
<!--         <script type="text/javascript">
        var cnzz_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
        document.write(unescape("%3Cspan id='cnzz_stat_icon_1278548644'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "v1.cnzz.com/stat.php%3Fid%3D1278548644%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));
        </script> -->
    </p>
</div>
<input type="hidden" id="web_style" value="black">
<input type="hidden" id="valine_appid" value="oU6aM6U9xOv4LHEQLo3LLROT-gzGzoHsz">
<input type="hidden" id="valine_appKey" value="TbfimrkuMCSYC9BPMRdOqKga">

<script src="/libs/jquery.min.js"></script>


<script src="/libs/highlight/highlight.pack.js"></script>

<script src='//cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>

<script src="/js/js.js"></script>

<style type="text/css">
.v * {
    color: #698fca;
}

.v .vlist .vcard .vhead .vsys {
    color: #3a3e4a;
}

.v .vlist .vcard .vh .vmeta .vat {
    color: #638fd5;
}

.v .vlist .vcard .vhead .vnick {
    color: #6ba1ff;
}

.v a {
    color: #8696b1;
}

.v .vlist .vcard .vhead .vnick:hover {
    color: #669bfc;
}
</style>
</body>

</html>